﻿// 扩展函数
app.func = {};

// loading
app.func.loading = (target) => {
    return app.that.$loading({ target, background: "rgba(255, 255, 255, 0.5)" });
};

// loading 需要多次关闭
app.func.loadingSeveral = (target, total) => {
    return {
        loading: app.func.loading(target),
        total, count: 0, close() {
            this.count++; if (this.count >= total) {
                this.loading.close();
            }
        }
    };
};

// 登录超时
app.func.ajaxLogout = (text) => {
    var result = JSON.parse(text);
    if (result && result.data == "logout") {
        app.func.warning("登录超时，请重新登录").then(() => {
            location.href = "/work/home/authorize";
        });
    }
};

// ajax
app.func.ajax = (options) => {
    var loading;
    $.ajax({
        url: options.url,
        type: options.type || "get",
        data: options.data || {},
        dataType: options.dataType || "json",
        contentType: options.contentType || "application/x-www-form-urlencoded;charset=UTF-8",
        cache: options.cache || true,
        async: options.async == null ? true : options.async,
        beforeSend() {
            if (options.target) {
                loading = app.func.loading(options.target);
            }
            if (options.beforeSend) { options.beforeSend(); }
        },
        complete() {
            if (options.target) { loading.close(); }
            if (options.complete) { options.complete(); }
        },
        success(result) {
            if (options.success) { options.success(result); }
        },
        error(request) {
            if (options.error) { options.error(request); }
            if (request.status != 400) {
                app.func.error("系统错误，请查看错误日志");
                console.error(request);
            }
            else { app.func.ajaxLogout(request.responseText); }
        }
    });
};

// get
app.func.get = (url, data, target, success, async) => {
    app.func.ajax({ url, type: "get", data, target, success, async });
};

// post
app.func.post = (url, data, target, success, async) => {
    app.func.ajax({ url, type: "post", data, target, success, async });
};

// 长 loading
app.func.longLoading = (msg) => {
    msg = msg || "数据正在处理中，可能需要几分钟，请您耐心等候...";
    app.that.$msgbox({
        showClose: false,
        showConfirmButton: false,
        closeOnClickModal: false,
        closeOnPressEscape: false,
        closeOnHashChange: false,
        center: true,
        customStyle: { maxWidth: "580px" },
        message: Vue.h("div", { style: { width: "500px", padding: "10px 0" } }, [
            Vue.h("div", { style: { padding: "15px 0 20px", textAlign: "center", fontSize: "16px" } }, msg),
            Vue.h(ElementPlus.ElProgress, { percentage: 100, strokeWidth: 20, striped: true, stripedFlow: true, duration: 20, showText: false })
        ])
    });
};

// 长 loading 关闭
app.func.longLoadingClose = () => {
    app.that.$msgbox.close();
};

// 确认操作框
app.func.confirm = (message) => {
    return app.that.$confirm(message, "提示", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning", draggable: true });
};

// 消息提醒
app.func.message = (message, type) => {
    return app.that.$message({ message, type: type || "success" });
};

// 提示框
app.func.alter = (message, type) => {
    return app.that.$alert(message, "提示", { type, draggable: true, dangerouslyUseHTMLString: true });
};

// 提示框 success
app.func.success = (message) => {
    return app.func.alter(message, "success");
};

// 提示框 info
app.func.info = (message) => {
    return app.func.alter(message, "info");
};

// 提示框 warning
app.func.warning = (message) => {
    return app.func.alter(message, "error");
};

// 提示框 error
app.func.error = (message) => {
    return app.func.alter(message, "error");
};

// 表单初始化多个字段的加载处理
app.func.formInit = (inits) => {
    app.that.$nextTick(() => {
        var loading = app.func.loadingSeveral(".el-dialog", inits.length);
        inits.forEach(m => m(loading));
    });
};

// 读取模型
app.func.getEntity = (url, id, success, fail) => {
    app.that.$nextTick(() => {
        app.func.get(url, { id }, ".el-dialog", result => {
            if (result.success) {
                if (success) { success(result); }
            }
            else {
                app.func.warning(result.message);
                if (fail) { fail(result); }
            }
        });
    });
};

// 读取数据
app.func.getData = (url, data, target, success, fail, async) => {
    app.func.get(url, data, target, result => {
        if (result.success) {
            if (success) { success(result); }
        }
        else {
            app.func.warning(result.message);
            if (fail) { fail(result); }
        }
    }, async);
};

// 提交表单
app.func.submit = (url, record, success, fail) => {
    app.func.dialogAjaxDo(url, record, success, fail);
};

// 删除操作
app.func.delete = (url, ids, success, fail) => {
    app.func.pageConfirmDo(url, "是否确认删除?", { ids }, success, fail);
};

// 表格行编辑验证
app.func.tableRowEditVerify = (row, rules) => {
    var errArr = [];
    for ([name, value] of Object.entries(row)) {
        if (!value && rules[name] && rules[name][0] && rules[name][0].required) {
            errArr.push(`${errArr.length + 1}、${rules[name][0].message}`);
        }
    }
    if (errArr.length) {
        app.func.warning(errArr.join("<br/>"));
    }
    return !errArr.length;
};

// 表格行编辑单元格验证
app.func.tableRowEditCellVerify = (scope, rules) => {
    if (scope && scope.column && scope.column.property) {
        if (scope.row && !scope.row[scope.column.property]) {
            if (rules[scope.column.property] && rules[scope.column.property][0] && rules[scope.column.property][0].required) {
                return false;
            }
        }
    }
    return true;
};

// 页面附属操作
app.func.pageConfirmDo = (url, message, data, success, fail) => {
    app.func.confirm(message).then(() => {
        app.func.pageAjaxDo(url, data, success, fail);
    }).catch(() => { });
};

// 页面附属操作
app.func.pageAjaxDo = (url, data, success, fail) => {
    app.func.post(url, data, ".layout-page-component", result => {
        if (result.success) {
            app.func.message(result.message);
            if (success) { success(result); }
        }
        else {
            app.func.warning(result.message);
            if (fail) { fail(result); }
        }
    });
};

// 弹窗带确认附属操作
app.func.dialogConfirmDo = (url, message, data, success, fail) => {
    app.func.confirm(message).then(() => {
        app.func.dialogAjaxDo(url, data, success, fail);
    }).catch(() => { });
};

// 弹窗附属操作
app.func.dialogAjaxDo = (url, data, success, fail) => {
    app.func.post(url, data, ".el-dialog", result => {
        if (result.success) {
            app.func.message(result.message);
            if (success) { success(result); }
        }
        else {
            app.func.warning(result.message);
            if (fail) { fail(result); }
        }
    });
};

// 长请求带确认
app.func.longConfirmDo = (url, message, data, loadingMsg, success, fail) => {
    app.func.confirm(message).then(() => {
        app.func.longAjaxDo(url, data, loadingMsg, success, fail);
    }).catch(() => { });
};

// 长请求
app.func.longAjaxDo = (url, data, loadingMsg, success, fail) => {
    app.func.ajax({
        url, type: "post", data,
        beforeSend() {
            app.func.longLoading(loadingMsg);
        },
        complete() {
            app.func.longLoadingClose();
        },
        success(result) {
            app.that.$nextTick(() => {
                if (result.success) {
                    app.func.success(result.message);
                    if (success) { success(result); }
                }
                else {
                    app.func.warning(result.message);
                    if (fail) { fail(result); }
                }
            });
        }
    });
};

// 下载文件
app.func.download = (src, filename) => {
    var link = document.createElement('a');
    link.href = src;
    link.download = filename;
    link.click();
};

// 导出 excel
app.func.excelSmall = (url, data) => {
    app.func.pageAjaxDo(url, data, result => {
        app.func.download(result.data.file, result.data.name);
    });
};

// 导出 excel
app.func.excelConfirm = (url, message, data, loadingMsg) => {
    app.func.longConfirmDo(url, message, data, loadingMsg, result => {
        app.func.download(result.data.file, result.data.name);
    });
};

// 导出 excel
app.func.excelLong = (url, data, loadingMsg) => {
    app.func.longAjaxDo(url, data, loadingMsg, result => {
        app.func.download(result.data.file, result.data.name);
    });
};

// 组装树形
app.func.structureTree = (data, pid) => {
    var nodes = data.filter(m => { return m.Pid == pid });
    nodes.forEach(node => {
        node.children = app.func.structureTree(data, node.Id);
    });
    return nodes;
};

// 组装树形自定义筛选条件
app.func.structureFilterTree = (data, pid, filter) => {
    var nodes = data.filter(m => filter(m, pid));
    nodes.forEach(node => {
        node.children = app.func.structureFilterTree(data, node.Id, filter);
    });
    return nodes;
};